// book base controller
var app = angular.module('app');

app.controller('bookBaseCtrl', ['$scope', '$state', '$translate', '$window',
    'bookService', 'confirmDialogService', 'CONSTANTS', 'toasterService', 'errorUtilsService',
    function ($scope, $state, $translate, $window,
              bookService, confirmDialogService, CONSTANTS, toasterService, errorUtilsService) {
        $scope.addAuthor = function (book) {
            if (!book) {
                return;
            }

            book.author = book.author || [];
            book.author.push('');
        };
        $scope.delAuthor = function (book, index) {
            if (!book || !book.author) {
                return;
            }
            confirmDialogService.showDialog($translate.instant('strings.dialog.title'),
                $translate.instant('strings.book.author.delete'))
                .then(function () {
                    book.author.splice(index, 1);
                });
        };

        $scope.addTranslator = function (book) {
            if (!book) {
                return;
            }

            book.translator = book.translator || [];
            book.translator.push('');
        };
        $scope.delTranslator = function (book, index) {
            if (!book || !book.translator) {
                return;
            }
            confirmDialogService.showDialog($translate.instant('strings.dialog.title'),
                $translate.instant('strings.book.translator.delete'))
                .then(function () {
                    book.translator.splice(index, 1);
                });
        };
        $scope.addTag = function (book) {
            if (!book) {
                return;
            }

            book.tags = book.tags || [];
            book.tags.push({});
        };
        $scope.delTag = function (book, index) {
            if (!book || !book.tags) {
                return;
            }
            confirmDialogService.showDialog($translate.instant('strings.dialog.title'),
                $translate.instant('strings.book.tag.delete'))
                .then(function () {
                    book.tags.splice(index, 1);
                });
        };
        $scope.filterAuthorTranslatorTag = function (book) {
            if (book.author) {
                for (var i = book.author.length - 1; i >= 0; i--) {
                    if (!book.author[i] || book.author[i].trim() === '') {
                        book.author.splice(i, 1);
                    }
                }
            }
            if (book.translator) {
                for (var i = book.translator.length - 1; i >= 0; i--) {
                    if (!book.translator[i] || book.translator[i].trim() === '') {
                        book.translator.splice(i, 1);
                    }
                }
            }
            if (book.tags) {
                for (var i = book.tags.length - 1; i >= 0; i--) {
                    if (!book.tags[i].name || book.tags[i].name.trim() === '') {
                        book.tags.splice(i, 1);
                    }
                }
            }
        };

    }]);